<template>
  <van-cell center title="心动模式">
    <template #right-icon>
      <van-switch v-model="isMatchMode" size="24" />
    </template>
  </van-cell>
  <div v-if="loading" class="data-loading">
    <van-skeleton title avatar :row="3" :loading="loading"></van-skeleton>
  </div>
  <user-card-list :user-list="userList" :loading="loading"/>
  <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
</template>

<script setup lang="ts">
import { ref, watchEffect } from 'vue';
import myAxios from "../plugins/myAxios";
import UserCardList from "../components/UserCardList.vue";


const isMatchMode = ref<boolean>(false);

const userList = ref([]);
const loading = ref(true);


/**
 * 加载数据
 */
const loadData = async () => {
  let userListData;
  loading.value = true;
  // 心动模式，根据标签匹配用户
  if (isMatchMode.value) {
    const num = 5;
    userListData = await myAxios.get('/user/match', {
      params: {
        num,
      },
    })
        .then(function (response) {
          console.log('/user/match succeed', response);
          return response?.data;
        })
        .catch(function (error) {
          console.error('/user/match error', error);
        })
  } else {
    // 普通模式，直接分页查询用户
    userListData = await myAxios.get('/user/recommend', {
      params: {
        pageSize: 10,
        pageNum: 1
      }
    })
        .then(function (response) {
          console.log('/user/recommend succeed', response);
          return response?.data?.records;
        })
        .catch(function (error) {
          console.error('/user/recommend error', error);

        })
  }
  if (userListData) {
    userListData.forEach((user) => {
      if (user.tags) {
        user.tags = JSON.parse(user.tags);
      }
    })
    userList.value = userListData;
  }
  console.log(userList)
  loading.value = false;
}

watchEffect(() => {
  loadData();
})

</script>

<style scoped>
.data-loading {
  position: fixed; /* 覆盖整个页面 */
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex; /* Flex 居中 */
  justify-content: center;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.8); /* 可选：加半透明遮罩 */
  z-index: 9999;
}/* 确保覆盖在最上层 */
</style>
